package com.cuitrip.business.location.map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.cuitrip.service.R;

import java.util.ArrayList;

/**
 * Created by MiJack on 2015/12/11.
 */
public class MapAdapter extends BaseAdapter {
    private ArrayList<PoiItem> result;

    public void setResult(PoiResult result) {
        if (result.getPois().size() == 0) {
            return;
        }
        this.result = result.getPois();
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return result == null ? 0 : result.size();
    }

    @Override
    public Object getItem(int position) {
        return result.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.ct_checkable_list_item, parent, false);
            holder.textView = (TextView) convertView.findViewById(R.id.simple_text);holder.textView.setSingleLine();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PoiItem item = result.get(position);
        holder.textView.setText(item.getTitle());
        holder.poiItem = item;
        return convertView;
    }

    public static class ViewHolder {
        public PoiItem poiItem;
        public TextView textView;
    }
}
